#include  <mqueue.h>
#include  <stdio.h>
 
int main(int argc, char const *argv[])
{
    mq_unlink("/mq_test");
    struct  mq_attr attr ;
    attr.mq_msgsize=128;
    attr.mq_maxmsg=100;
    
    //1  创建一个消息队列  mq_open()
    mqd_t mq = mq_open("/mq_test", O_RDWR | O_CREAT, 0666, &attr);
    if(mq == -1)
    {
        perror("mq_open error");  
        return -1;
    }

    //2  向队列中写入数据  mq_send()
    mq_send(mq, "hello world", 11, 0);


    //3  从队列中读取数据  mq_receive()
    char buf[128] = {0};
    ssize_t data_size=  mq_receive(mq, buf, 128, 0);

    printf("data_size=%ld, buf=%s\n", data_size, buf);

    //4 销毁队列  mq_close()  mq_unlink()
    mq_close(mq);
    //mq_unlink("/mq_test");
    
    return 0;
}
